package com.project.psc.util;

import com.alibaba.fastjson.JSONObject;
import com.project.psc.dto.SupplierDto;
import com.project.util.Verifier;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        if (token == null || token.length() == 0) {
            response.getWriter().print("noland");
            return false;
        }

        String json = Verifier.verifierJwt(token);
        SupplierDto loginUser = JSONObject.parseObject(json, SupplierDto.class);
        SupplierThreadLocal.saveSupplier(loginUser);
        
        return true;
    }

}
